
#include "hj212_client_def.h"
#include "hj212_base_request.h"
#include "hj212_msg.h"
#include "hj212_pack.h"
#include "hj212_client_ops.h"

/* *
 * 所有基本请求
 *
 * 简单的请求可以不必创建request结构体
 */

/* 1000 - 设置超时时间和重发次数 -  */
int hj212_request_1000_match(struct hj212_client *client, struct hj212_generalmsg *msg, uint32_t package_state)
{
    int res = hj212_standard_request_preprocessing(client, msg, package_state, 1000);
    if (res < 0)
    {
        return res;
    }
    if (!res)
    {
        const char *OverTime_p = strstr(msg->cp, "OverTime=");
        const char *ReCount_p = strstr(msg->cp, "ReCount=");
        if (!OverTime_p || !ReCount_p)
        {
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_INVALID_PARAM, msg);
            return 0;
        }
        int OverTime = atoi(OverTime_p + strlen("OverTime="));
        int ReCount = atoi(ReCount_p + strlen("ReCount="));

        int res1 = hj212_client_command(client, SET_HJ212_STREAM_TIMEOUT, &OverTime);
        int res2 = hj212_client_command(client, SET_HJ212_MAXRET, &ReCount);

        if (!res1 && !res2)
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_SUCCEED, msg);
        else
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_UNKNOWN_ERROR, msg);
    }
    return 0;
}

/* 1011 提取现场机时间 - 暂时不支持操作下级设备 */
int hj212_request_1011_match(struct hj212_client *client, struct hj212_generalmsg *msg, uint32_t package_state)
{
    int res = hj212_standard_request_preprocessing(client, msg, package_state, 1011);
    if (res < 0)
    {
        return res;
    }
    if (msg->parent.cn != 1011)
    {
        return -1;
    }
    if (!res)
    {
        struct tm time;
        time_t current = hj212_client_current_timestamp(client);
        if (localtime_r(&current, &time))
        {
            hj212_send_local_pack(client, msg->parent.qn, NULL, 1011, 0, NULL,
                                  "SystemTime=%04d%02d%02d%02d%02d%02d;",
                                  (time).tm_year + 1900, (time).tm_mon + 1, (time).tm_mday,
                                  (time).tm_hour, (time).tm_min, (time).tm_sec);
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_SUCCEED, msg);
        }
        else
        {
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_FAULT, msg);
        }
    }
    return 0;
}

/* 1012 设置现场机时间 - 暂时不支持操作下级设备 */
int hj212_request_1012_match(struct hj212_client *client, struct hj212_generalmsg *msg, uint32_t package_state)
{
    int res = hj212_standard_request_preprocessing(client, msg, package_state, 1012);
    if (res < 0)
    {
        return res;
    }
    if (!res)
    {
        const char *p = strstr(msg->cp, "SystemTime=");
        if (!p)
        {
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_INVALID_PARAM, msg);
            return 0;
        }
        struct tm system_time;
        int res = sscanf(p, "SystemTime=%04d%02d%02d%02d%02d%02d",
                         &system_time.tm_year, &system_time.tm_mon, &system_time.tm_mday,
                         &system_time.tm_hour, &system_time.tm_min, &system_time.tm_sec);
        if (res != 6)
        {
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_INVALID_PARAM, msg);
            return 0;
        }
        system_time.tm_year -= 1900;
        system_time.tm_mon -= 1;
        if (hj212_client_set_current_timestamp(client, mktime(&system_time)) < 0)
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_UNKNOWN_ERROR, msg);
        else
            hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_SUCCEED, msg);
    }
    return 0;
}

/* 2011 启动实时数据 - 请求 */
int hj212_request_2011_match(struct hj212_client *client, struct hj212_generalmsg *msg, uint32_t package_state)
{
    int res = hj212_standard_request_preprocessing(client, msg, package_state, 2011);
    if (res < 0)
    {
        return res;
    }
    if (!res)
    {
        client->push_enable |= (1 << HJ212_MSG_TYPE_RTD);  // 打开实时数据
        hj212_commit_request_result_frommsg(client, REQUEST_EXE_RESULT_SUCCEED, msg);
    }
    return 0;
}

/* 2012 关闭实时数据 */
int hj212_request_2012_match(struct hj212_client *client, struct hj212_generalmsg *msg, uint32_t package_state)
{
    if (msg->parent.cn != 2012)
    {
        return -1;
    }
    enum request_response response = hj212_check_request_msg(client, msg, package_state);
    if (response == REQUEST_RESPONSE_ACCEPT)
    {
        client->push_enable &= ~(1 << HJ212_MSG_TYPE_RTD);  // 关闭实时数据
        hj212_send_local_pack(client, msg->parent.qn, "91", 9013, 0, NULL, NULL);
    }
    return 0;
}
